/***
 * 自定义用户
 * */


//列表
function getIndex(){
    var formData = {
        aid: getAid(),
        pageStart: pages.start,
        pageLimit: pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.shopIndex.method,
        url: apis.shopIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].shop_name + '</td>';
        h += '<td>' + d[i].shop_code + '</td>';
        if (d[i].headimg == '') {
            h += '<td>-</td>';
        } else {
            h += '<td><img src="' + d[i].headimg + '" height="50"/></td>';
        }
        h += '<td>' + d[i].code + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="lay_edit(\'编辑\',\'shop-edit.html\', ' + d[i].id + ');">';
        h += '编辑 <i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '<br>';
        h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += '删除';
        h += '</a>';
        h += '</a>';
        h += '</tr>';
    }
    return h;
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,500);
}


//获取详情
function getOne(v){
    $.ajax({
        type: apis.shopOne.method,
        url: apis.shopOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            $('#sname')[0].value = d.shop_name;
            $('#sdesc')[0].value = d.describe;
            if (d.headimg != '') {
                showImg({img: d.headimg});
            }
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//更新
function modify(){
    var formData = {
        id: id,
        sname: $('#sname').val(),
        sdesc: $('#sdesc').val(),
        img: $('#img').val(),
    };
    $.ajax({
        type: apis.shopModify.method,
        url: apis.shopModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//删除
function lay_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.shopDel.method,
            url: apis.shopDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layer.msg(res.msg,{icon: 0,time: 1000});
                    //msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}